﻿using System.Web.Configuration;
using System.Linq;
using System;

namespace JQuery
{
    public static class Config
    {
        private static JQNConfigSection Section;
        static Config()
        {
            try
            {
                Section = (JQNConfigSection)WebConfigurationManager.GetSection(typeof(JQNConfigSection).Name);
            }
            catch
            {
                Section = new JQNConfigSection();
            }
        }

        public static string JQueryAPI
        {
            get
            {
                string @default = "1.10.2", api = "";

                api = Section.Configuration[PluginConfigSection.jquery].IsNotNull(@default, y => y.Settings["folder"].IsNotNull(@default, x => x.Value));

                return api.IsNotNullOrEmpty() ? api : @default;
            }
        }

        public static string ScriptFolder
        {
            get
            {
                string @default = "~/scripts", folder = "";

                folder = Section.Configuration[PluginConfigSection.jquery].IsNotNull(@default, y => y.Settings["scripts-folder"].IsNotNull(@default, x => x.Value));

                return folder.IsNotNullOrEmpty() ? folder : @default;
            }
        }

        public static string CssFolder
        {
            get
            {
                string @default = "~/css", folder = "";

                folder = Section.Configuration[PluginConfigSection.jquery].IsNotNull(@default, y => y.Settings["css-folder"].IsNotNull(@default, x => x.Value));

                return folder.IsNotNullOrEmpty() ? folder : @default;
            }
        }

        public static string[] Localization
        {
            get
            {
                string[]
                    @default = new string[] { "ne" },
                    actual = new string[] { };

                actual = Section.Configuration[PluginConfigSection.jquery].IsNotNull(@default, x => x.Settings["localization"].IsNotNull(@default, y => y.Value.Split(',')));

                return actual.Length > 0 ? actual : @default;
            }
        }

        public static bool LoadAllLanguageFiles
        {
            get
            {   // if the localization  neutral is listed then we need to load all language files available.
                return Localization.Any(x => x.Eq("ne"));
            }
        }

        public static bool MVCEnabled
        {
            get
            {
                bool @default = false;

                return Section.Configuration[PluginConfigSection.jquery].IsNotNull(@default, x => x.Settings["mvc"].IsNotNull(@default, y => Convert.ToBoolean(y.Value)));
            }
        }

        public static bool EnableMVCSchemaEngine
        {
            get
            {
                bool @default = false;

                return MVCEnabled && Section.Configuration[PluginConfigSection.mvc].IsNotNull(@default, x => x.Settings["enable-schema-engine"].IsNotNull(@default, y => Convert.ToBoolean(y.Value)));
            }
        }

        public static bool CSSThemeOverride
        {
            get
            {
                bool @default = false;

                return Section.Configuration[PluginConfigSection.jquery].IsNotNull(@default, x => x.Settings["css-theme"].IsNotNull(@default, y => Convert.ToBoolean(y.Value)));
            }
        }

        public static bool IsJQueryUIAvailble
        {
            get
            {
                string @default = "false", value = "";

                value = Section.Configuration[PluginConfigSection.jquery].IsNotNull(@default, y => y.Settings["ui"].IsNotNull(@default, x => x.Value));

                return value.Eq("true");
            }
        }

        public static bool IsMinimized { get { return Section.IsMinimized; } }

        public static bool IsOptimized { get { return Section.IsOptimized; } }

        public static bool IsPluginsEnabled { get { return Section.EnablePlugins; } }
    }
}
